package com.jic.member.impl;

import com.jic.common.base.vo.RestResult;
import com.jic.member.entity.*;
import com.jic.member.enums.EXCEPTION;
import com.jic.member.mapper.*;
import com.jic.member.request.MemberIntegralRestRequest;
import com.jic.member.request.MememberIntegralActionRequest;
import com.jic.member.request.MememberIntegralRequest;
import com.jic.member.service.MememberActionNewService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

import static java.math.BigDecimal.ROUND_DOWN;

/**
 * 会员业务主表
 *
 * @author :
 * @version : v1.0.0
 * @email :
 * @since : 2020-04-17 10:45:11
 */
@Slf4j
@Service
public class MememberActionNewServiceImpl implements MememberActionNewService {

    @Resource
    private MememberIntegralLogMapper mememberIntegralLogMapper;

    @Autowired
    private MememberIntegralRuleMapper mememberIntegralRuleMapper;

    @Autowired
    private MememberIntegralOrderRechargeMapper mememberIntegralOrderRechargeMapper;

    @Autowired
    private MememberIntegralGiveConsumeMapper mememberIntegralGiveConsumeMapper;

    @Autowired
    private MememberIntegralActionMapper mememberIntegralActionMapper;
    @Autowired
    private MememberIntegralOrderIncrementMapper mememberIntegralOrderIncrementMapper;
    @Resource
    private MememberGrowthAppraisalMapper mememberGrowthAppraisalMapper;
    @Resource
    private MememberIntegralBaseMapper mememberIntegralBaseMapper;


    @Resource
    private MememberUserMapper mememberUserMapper;


    /**
     * 更新会员积分
     *
     * @param memberIntegralRestRequest
     * @return
     */
    @Override
    public RestResult<?> addMemberIntegralInfoNew(MemberIntegralRestRequest memberIntegralRestRequest) {
        RestResult restResult = new RestResult();
        if (memberIntegralRestRequest.getUserId() == null) {
            return RestResult.error(EXCEPTION.FAIL.getName(), "用户id不能为空");
        }

        MememberUser mMemberVo = mememberUserMapper.queryByPrimaryKey(memberIntegralRestRequest.getUserId());

        if (mMemberVo == null) {
            return RestResult.error(EXCEPTION.FAIL.getName(), "用户不存在");
        }

        if (mMemberVo.getDeleteFlag() == 1) {
            return RestResult.error("9999", "用户被冻结，请联系管理员");
        }
        Integer status = mMemberVo.getStatus();
        if (status == 0) {
            return RestResult.error("9999", "当前账号已注销");
        }

        if (memberIntegralRestRequest.getChangeTypes() == null) {
            return RestResult.error(EXCEPTION.FAIL.getName(), "changeTypes不能为空");
        }
        //查询当天的积分总数
        BigDecimal totalNumberOfPoints = mememberIntegralLogMapper.queryTotalNumberOfPointsOnDay(new MememberIntegralLog() {{
            setChangeTypes(memberIntegralRestRequest.getChangeTypes());
            setMemberUserId(memberIntegralRestRequest.getUserId());
            setCreateTime(new Date());
        }});

        MememberIntegralBase mememberIntegralBase = new MememberIntegralBase();
        mememberIntegralBase.setDeleteFlag(0);
        mememberIntegralBase.setStatus(1);
        final MememberIntegralBase query = mememberIntegralBaseMapper.queryByDeptId(mememberIntegralBase);
        if (memberIntegralRestRequest.getChangeTypes() == 1) {
            if (memberIntegralRestRequest.getObtainMode() == null) {
                return RestResult.error(EXCEPTION.FAIL.getName(), "积分获取方式不能空");
            }
            //积分规则配置主表
            List<MememberIntegralRule> mememberIntegralRuleList = mememberIntegralRuleMapper.query(new MememberIntegralRule() {{
                setObtainMode(memberIntegralRestRequest.getObtainMode() + "");
                setStatus(1);
            }});
            if (CollectionUtils.isEmpty(mememberIntegralRuleList)) {
                return RestResult.error(EXCEPTION.FAIL.getName(), "积分规则配置为空");
            }
            if (query.getDailyCeiling() != null) {
                if ((totalNumberOfPoints == null ? BigDecimal.ZERO : totalNumberOfPoints).
                        compareTo(query.getDailyCeiling()) != -1) {
                    return RestResult.error(EXCEPTION.FAIL.getName(), "赠送积分超出每日上限");
                }
            }
            BigDecimal totalNumberOfPointsChild = mememberIntegralLogMapper.queryTotalNumberOfPointsOnDay(new MememberIntegralLog() {{
                setChangeTypes(memberIntegralRestRequest.getChangeTypes());
                setMemberUserId(memberIntegralRestRequest.getUserId());
                setCreateTime(new Date());
                setChangeMode(memberIntegralRestRequest.getObtainMode());
            }});
            //获取方式:1:下单赠送2:注册赠送3:评价赠送4:充值赠送5:分享赠送6:点赞赠送7:晒单赠送8:收藏赠送
            switch (memberIntegralRestRequest.getObtainMode() + "") {
                case "1":
                    restResult = this.orderToMakeToSend(mememberIntegralRuleList, totalNumberOfPointsChild,
                            memberIntegralRestRequest, mMemberVo, query, totalNumberOfPoints);
                    break;
                case "2":
                    restResult = this.registerFree(mememberIntegralRuleList, memberIntegralRestRequest,
                            mMemberVo, query, totalNumberOfPoints);
                    break;
                case "3":
                    restResult = this.evaluationOfPresent(mememberIntegralRuleList, totalNumberOfPointsChild,
                            memberIntegralRestRequest, mMemberVo, query, totalNumberOfPoints);
                    break;
                case "4":
                    restResult = this.topupGifts(mememberIntegralRuleList, totalNumberOfPointsChild,
                            memberIntegralRestRequest, mMemberVo, query, totalNumberOfPoints);
                    break;
                case "5":
                    restResult = this.shareGifts(mememberIntegralRuleList, totalNumberOfPointsChild,
                            memberIntegralRestRequest, mMemberVo, query, totalNumberOfPoints);
                    break;
                case "6":
                    restResult = this.thumbUpPresent(mememberIntegralRuleList, totalNumberOfPointsChild,
                            memberIntegralRestRequest, mMemberVo, query, totalNumberOfPoints);
                    break;
                case "7":
                    restResult = this.baskInASingleGift(mememberIntegralRuleList, totalNumberOfPointsChild,
                            memberIntegralRestRequest, mMemberVo, query, totalNumberOfPoints);
                    break;
                case "8":
                    restResult = this.collection(mememberIntegralRuleList, totalNumberOfPointsChild,
                            memberIntegralRestRequest, mMemberVo, query, totalNumberOfPoints);
                    break;
            }
        } else if (memberIntegralRestRequest.getChangeTypes() == 2) {
            if (memberIntegralRestRequest.getConsumeMode() == null) {
                return RestResult.error(EXCEPTION.FAIL.getName(), "消耗方式方式不能空");
            }
            //积消耗方式分规则配置主表
            List<MememberIntegralRule> mememberIntegralRuleList = mememberIntegralRuleMapper.query(new MememberIntegralRule() {{
                setConsumeMode(memberIntegralRestRequest.getConsumeMode() + "");
                setStatus(1);
            }});
            if (CollectionUtils.isEmpty(mememberIntegralRuleList)) {
                return RestResult.error(EXCEPTION.FAIL.getName(), "积消耗方式分规则配置为空");
            }
            //消耗方式:1:消费抵现2:兑换优惠券 3.兑换红包
            switch (memberIntegralRestRequest.getConsumeMode() + "") {
                case "1":
                    restResult = this.consumptionForNow(memberIntegralRestRequest, mMemberVo,
                            query, mememberIntegralRuleList);
                    break;
                case "2":
                    //TODO 需求pending
                    break;
                case "3":
                    //TODO 需求pending
                    break;
            }
        }
        return restResult;
    }

    /**
     * 查询会员积分详情
     *
     * @param request
     * @return
     * @author zxl
     */
    @Override
    public RestResult<?> getMemberIntegralInfo(MememberIntegralRequest request) {
        log.info("开始查询会员积分详情==》入参,{}", request.toString());
        RestResult restResult = new RestResult();

        return restResult;
    }


    /**
     * 新增会员积分接口
     *
     * @param mememberIntegralActionRequest
     * @return
     */
    @Override
    public RestResult<?> updateMemberIntegralInfo(MememberIntegralActionRequest mememberIntegralActionRequest) {
        RestResult restResult = new RestResult();
        return restResult;
    }

    /**
     * 下单赠送表
     *
     * @param mememberIntegralRuleList
     * @param totalNumberOfPoints
     * @param memberIntegralRestRequest
     * @return
     */
    public RestResult orderToMakeToSend(List<MememberIntegralRule> mememberIntegralRuleList, BigDecimal totalNumberOfPoints,
                                        MemberIntegralRestRequest memberIntegralRestRequest, MememberUser
                                                mMemberVo, MememberIntegralBase mememberIntegralBase, BigDecimal totalScore) {
        //会员积分规则下单-充值赠送表
        MememberIntegralOrderRecharge mememberIntegralOrderRecharge =
                mememberIntegralOrderRechargeMapper.queryInfoByRuleId(mememberIntegralRuleList.get(0).getRuleId());
        if (mememberIntegralOrderRecharge == null) {
            return RestResult.error(EXCEPTION.FAIL.getName(), "下单赠送失败,积分规则配置为空");
        }
        if ((totalNumberOfPoints == null ? new BigDecimal(0) : totalNumberOfPoints).
                compareTo(mememberIntegralOrderRecharge.getGiveIntegralMaxNum()) != -1) {
            return RestResult.error(EXCEPTION.FAIL.getName(), "下单赠送失败,赠送积分超出每日上限");
        }
        RestResult restResult = orderOrRecharge(memberIntegralRestRequest, mMemberVo, mememberIntegralOrderRecharge,
                "下单赠送", totalNumberOfPoints, mememberIntegralBase, totalScore);
        return restResult;
    }

    /**
     * 注册赠送
     *
     * @param mememberIntegralRuleList
     * @param memberIntegralRestRequest
     * @return
     */
    public RestResult registerFree(List<MememberIntegralRule> mememberIntegralRuleList, MemberIntegralRestRequest
            memberIntegralRestRequest, MememberUser mMemberVo, MememberIntegralBase mememberIntegralBase, BigDecimal totalNumberOfPoints) {
        //查询注册规则
        MememberIntegralGiveConsume mememberIntegralGiveConsume = mememberIntegralGiveConsumeMapper.queryByRuleId(mememberIntegralRuleList.get(0).getRuleId());
        if (mememberIntegralGiveConsume == null) {
            return RestResult.error(EXCEPTION.FAIL.getName(), "注册赠送失败,积分规则配置为空");
        }
        //比较消耗积分上限
        RestResult restResult = integralActionType(memberIntegralRestRequest, mMemberVo, mememberIntegralGiveConsume.
                getConsumeIntegralMaxNum(), mememberIntegralGiveConsume.getGiveIntegralNum(), "注册赠送", mememberIntegralBase, totalNumberOfPoints, null);
        return restResult;
    }

    /**
     * 评价赠送
     *
     * @param mememberIntegralRuleList
     * @param totalNumberOfPoints
     * @param memberIntegralRestRequest
     * @return
     */
    public RestResult evaluationOfPresent(List<MememberIntegralRule> mememberIntegralRuleList, BigDecimal totalNumberOfPointsChild,
                                          MemberIntegralRestRequest memberIntegralRestRequest, MememberUser
                                                  mMemberVo,
                                          MememberIntegralBase mememberIntegralBase, BigDecimal totalNumberOfPoints) {
        MememberIntegralAction mememberIntegralAction = mememberIntegralActionMapper.
                queryByRuleId(mememberIntegralRuleList.get(0).getRuleId());
        if (memberIntegralRestRequest.getFigureFlag() == null) {
            return RestResult.error(EXCEPTION.FAIL.getName(), "评价赠送失败,figureFlag为空");
        }
        if (mememberIntegralAction == null) {
            return RestResult.error(EXCEPTION.FAIL.getName(), "评价赠送失败,积分规则配置为空");
        }
        if (mememberIntegralAction.getFlag() != null && mememberIntegralAction.getFlag() == 1) {
            if ((totalNumberOfPointsChild == null ? new BigDecimal(0) : totalNumberOfPointsChild).
                    compareTo(mememberIntegralAction.getGiveIntegralMaxNum()) != -1) {
                return RestResult.error(EXCEPTION.FAIL.getName(), "评价赠送失败,赠送积分超出每日上限");
            }
        } else if (mememberIntegralAction.getFlag() != null && mememberIntegralAction.getFlag() == 2) {
            if ((totalNumberOfPointsChild == null ? new BigDecimal(0) : totalNumberOfPointsChild).
                    compareTo(mememberIntegralAction.getGiveIntegralMaxNum()) != -1) {
                return RestResult.error(EXCEPTION.FAIL.getName(), "评价赠送失败,赠送积分超出每日上限");
            }
        } else if (mememberIntegralAction.getFlag() != null && mememberIntegralAction.getFlag() == 3) {
            List<MememberGrowthAppraisal> growthAppraisalList = mememberGrowthAppraisalMapper.query(new MememberGrowthAppraisal() {{
                setGrowthRuleId(mememberIntegralAction.getId());
                setDeleteFlag(0);
            }});
            if (CollectionUtils.isEmpty(growthAppraisalList)) {
                return RestResult.error(EXCEPTION.FAIL.getName(), "评价赠送失败,积分规则配置为空");
            }
            if (memberIntegralRestRequest.getFigureFlag() == 1) {
                if ((totalNumberOfPointsChild == null ? new BigDecimal(0) : totalNumberOfPointsChild).
                        compareTo(growthAppraisalList.get(0).getGiveGrowthMaxNum()) != -1) {
                    return RestResult.error(EXCEPTION.FAIL.getName(), "评价赠送失败,赠送积分超出每日上限");
                }
                mememberIntegralAction.setGiveIntegralMaxNum(growthAppraisalList.get(0).getGiveGrowthMaxNum());
                mememberIntegralAction.setGiveIntegralNum(growthAppraisalList.get(0).getGiveGrowthVal());
            } else {
                if ((totalNumberOfPointsChild == null ? new BigDecimal(0) : totalNumberOfPointsChild).
                        compareTo(growthAppraisalList.get(1).getGiveGrowthMaxNum()) != -1) {
                    return RestResult.error(EXCEPTION.FAIL.getName(), "评价赠送失败,赠送积分超出每日上限");
                }
                mememberIntegralAction.setGiveIntegralMaxNum(growthAppraisalList.get(1).getGiveGrowthMaxNum());
                mememberIntegralAction.setGiveIntegralNum(growthAppraisalList.get(1).getGiveGrowthVal());
            }
        } else {
            return RestResult.error(EXCEPTION.FAIL.getName(), "评价赠送失败,积分规则配置为空");
        }
        RestResult restResult = integralActionType(memberIntegralRestRequest, mMemberVo,
                mememberIntegralAction.getGiveIntegralMaxNum(), mememberIntegralAction.getGiveIntegralNum(),
                "评价赠送", mememberIntegralBase, totalNumberOfPoints, totalNumberOfPointsChild);
        return restResult;
    }

    /**
     * 充值赠送
     *
     * @param mememberIntegralRuleList
     * @param totalNumberOfPoints
     * @param memberIntegralRestRequest
     * @return
     */
    public RestResult topupGifts(List<MememberIntegralRule> mememberIntegralRuleList, BigDecimal totalNumberOfPoints,
                                 MemberIntegralRestRequest memberIntegralRestRequest,
                                 MememberUser
                                         mMemberVo, MememberIntegralBase mememberIntegralBase, BigDecimal totalScore) {
        //会员积分规则下单-充值赠送表
        MememberIntegralOrderRecharge mememberIntegralOrderRecharge1 =
                mememberIntegralOrderRechargeMapper.queryInfoByRuleId(mememberIntegralRuleList.get(0).getRuleId());
        if (mememberIntegralOrderRecharge1 == null) {
            return RestResult.error(EXCEPTION.FAIL.getName(), "充值赠送失败,积分规则配置为空");
        }
        if ((totalNumberOfPoints == null ? new BigDecimal(0) : totalNumberOfPoints).
                compareTo(mememberIntegralOrderRecharge1.getGiveIntegralMaxNum()) != -1) {
            return RestResult.error(EXCEPTION.FAIL.getName(), "充值赠送失败,赠送积分超出每日上限");
        }
        RestResult restResult = orderOrRecharge(memberIntegralRestRequest, mMemberVo,
                mememberIntegralOrderRecharge1, "充值赠送", totalNumberOfPoints, mememberIntegralBase, totalScore);
        return restResult;
    }

    /**
     * 分享赠送
     *
     * @param mememberIntegralRuleList
     * @param totalNumberOfPoints
     * @param memberIntegralRestRequest
     * @return
     */
    public RestResult shareGifts(List<MememberIntegralRule> mememberIntegralRuleList, BigDecimal totalNumberOfPointsChild,
                                 MemberIntegralRestRequest memberIntegralRestRequest, MememberUser
                                         mMemberVo,
                                 MememberIntegralBase mememberIntegralBase, BigDecimal totalNumberOfPoints) {
        MememberIntegralAction mememberIntegralAction1 = mememberIntegralActionMapper.
                queryByRuleId(mememberIntegralRuleList.get(0).getRuleId());
        if (mememberIntegralAction1 == null) {
            return RestResult.error(EXCEPTION.FAIL.getName(), "分享赠送失败,积分规则配置为空");
        }
        if ((totalNumberOfPointsChild == null ? new BigDecimal(0) : totalNumberOfPointsChild).
                compareTo(mememberIntegralAction1.getGiveIntegralMaxNum()) != -1) {
            return RestResult.error(EXCEPTION.FAIL.getName(), "分享赠送失败,赠送积分超出每日上限");
        }
        RestResult restResult = integralActionType(memberIntegralRestRequest, mMemberVo,
                mememberIntegralAction1.getGiveIntegralMaxNum(), mememberIntegralAction1.getGiveIntegralNum(),
                "分享赠送", mememberIntegralBase, totalNumberOfPoints, totalNumberOfPointsChild);
        return restResult;
    }

    /**
     * 点赞赠送
     *
     * @param mememberIntegralRuleList
     * @param totalNumberOfPoints
     * @param memberIntegralRestRequest
     * @return
     */
    public RestResult thumbUpPresent(List<MememberIntegralRule> mememberIntegralRuleList, BigDecimal totalNumberOfPointsChild,
                                     MemberIntegralRestRequest memberIntegralRestRequest, MememberUser
                                             mMemberVo,
                                     MememberIntegralBase mememberIntegralBase, BigDecimal totalNumberOfPoints) {
        MememberIntegralAction mememberIntegralAction2 = mememberIntegralActionMapper.
                queryByRuleId(mememberIntegralRuleList.get(0).getRuleId());
        if (mememberIntegralAction2 == null) {
            return RestResult.error(EXCEPTION.FAIL.getName(), "点赞赠送失败,积分规则配置为空");
        }
        if ((totalNumberOfPointsChild == null ? new BigDecimal(0) : totalNumberOfPointsChild).
                compareTo(mememberIntegralAction2.getGiveIntegralMaxNum()) != -1) {
            return RestResult.error(EXCEPTION.FAIL.getName(), "点赞赠送失败,赠送积分超出每日上限");
        }
        RestResult restResult = integralActionType(memberIntegralRestRequest, mMemberVo,
                mememberIntegralAction2.getGiveIntegralMaxNum(), mememberIntegralAction2.getGiveIntegralNum(),
                "点赞赠送", mememberIntegralBase, totalNumberOfPoints, totalNumberOfPointsChild);
        return restResult;
    }

    /**
     * 晒单赠送
     *
     * @param mememberIntegralRuleList
     * @param totalNumberOfPoints
     * @param memberIntegralRestRequest
     * @return
     */
    public RestResult baskInASingleGift(List<MememberIntegralRule> mememberIntegralRuleList, BigDecimal totalNumberOfPointsChild,
                                        MemberIntegralRestRequest memberIntegralRestRequest, MememberUser
                                                mMemberVo,
                                        MememberIntegralBase mememberIntegralBase, BigDecimal totalNumberOfPoints) {
        MememberIntegralAction mememberIntegralAction3 = mememberIntegralActionMapper.
                queryByRuleId(mememberIntegralRuleList.get(0).getRuleId());
        if (mememberIntegralAction3 == null) {
            return RestResult.error(EXCEPTION.FAIL.getName(), "晒单赠送失败,积分规则配置为空");
        }
        if ((totalNumberOfPointsChild == null ? new BigDecimal(0) : totalNumberOfPointsChild).
                compareTo(mememberIntegralAction3.getGiveIntegralMaxNum()) != -1) {
            return RestResult.error(EXCEPTION.FAIL.getName(), "晒单赠送失败,赠送积分超出每日上限");
        }
        RestResult restResult = integralActionType(memberIntegralRestRequest, mMemberVo,
                mememberIntegralAction3.getGiveIntegralMaxNum(), mememberIntegralAction3.getGiveIntegralNum(),
                "晒单赠送", mememberIntegralBase, totalNumberOfPoints, totalNumberOfPointsChild);
        return restResult;
    }

    /**
     * 收藏赠送
     *
     * @param mememberIntegralRuleList
     * @param totalNumberOfPoints
     * @param memberIntegralRestRequest
     * @return
     */
    public RestResult collection(List<MememberIntegralRule> mememberIntegralRuleList, BigDecimal totalNumberOfPointsChild,
                                 MemberIntegralRestRequest memberIntegralRestRequest, MememberUser
                                         mMemberVo,
                                 MememberIntegralBase mememberIntegralBase, BigDecimal totalNumberOfPoints) {
        MememberIntegralAction mememberIntegralAction4 = mememberIntegralActionMapper.
                queryByRuleId(mememberIntegralRuleList.get(0).getRuleId());
        if (mememberIntegralAction4 == null) {
            return RestResult.error(EXCEPTION.FAIL.getName(), "收藏赠送失败,积分规则配置为空");
        }
        if ((totalNumberOfPointsChild == null ? new BigDecimal(0) : totalNumberOfPointsChild).
                compareTo(mememberIntegralAction4.getGiveIntegralMaxNum()) != -1) {
            return RestResult.error(EXCEPTION.FAIL.getName(), "收藏赠送失败,赠送积分超出每日上限");
        }
        RestResult restResult = integralActionType(memberIntegralRestRequest, mMemberVo,
                mememberIntegralAction4.getGiveIntegralMaxNum(), mememberIntegralAction4.getGiveIntegralNum(), "收藏赠送",
                mememberIntegralBase, totalNumberOfPoints, totalNumberOfPointsChild);
        return restResult;
    }

    /**
     * 消费抵现
     *
     * @param memberIntegralRestRequest
     * @param mememberIntegralRuleList
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public RestResult consumptionForNow(MemberIntegralRestRequest memberIntegralRestRequest, MememberUser
            mMemberVo,
                                        MememberIntegralBase mememberIntegralBase, List<MememberIntegralRule> mememberIntegralRuleList) {
        Map<String, Object> map = new HashMap<>();
        if (memberIntegralRestRequest.getPlaceOrderAmount() == null) {
            return RestResult.error(EXCEPTION.FAIL.getName(), "下单金额不能为空");
        }

        if (mememberIntegralBase == null) {
            return RestResult.error(EXCEPTION.FAIL.getName(), "消费抵现失败,积分规则配置为空");
        }
        //消费抵现
        MememberIntegralGiveConsume mememberIntegralGiveConsume = mememberIntegralGiveConsumeMapper.
                queryByRuleId(mememberIntegralRuleList.get(0).getRuleId());
        if (mememberIntegralGiveConsume == null) {
            return RestResult.error(EXCEPTION.FAIL.getName(), "消费抵现失败,积分规则配置为空");
        }
        BigDecimal whenTimeToDeductPoints = BigDecimal.ZERO;
        //积分比例
        BigDecimal proportionIntegral = mememberIntegralBase.getRmb().divide(mememberIntegralBase.getIntegral());
        //先计算当日最大兑换积分
        BigDecimal consumptionIntegralNumber = proportionIntegral.multiply(memberIntegralRestRequest.getPlaceOrderAmount());
        //在判断用户积分够不够
        BigDecimal userIntegralNumber = mMemberVo.getIntegral().subtract(consumptionIntegralNumber);

        //用户积分大于或者等于的情况下判断有没有超出平台配置的每日上限积分数
        int falg = userIntegralNumber.compareTo(BigDecimal.ZERO);

        if (falg == 1 || falg == 0) {
            //超出上限
            if (consumptionIntegralNumber.compareTo(mememberIntegralGiveConsume.getConsumeIntegralMaxNum()) != -1) {
                //得到当次扣除积分
                whenTimeToDeductPoints = mememberIntegralGiveConsume.getConsumeIntegralMaxNum();
            } else {
                whenTimeToDeductPoints = consumptionIntegralNumber;
            }

        } else {
            if (mMemberVo.getIntegral().compareTo(mememberIntegralGiveConsume.getConsumeIntegralMaxNum()) == 1) {
                //得到当次扣除积分
                whenTimeToDeductPoints = mememberIntegralGiveConsume.getConsumeIntegralMaxNum();
            } else {
                return RestResult.error(EXCEPTION.FAIL.getName(), "消费抵现失败,用户积分不够");
            }
        }
        map.put("integral", whenTimeToDeductPoints);
        map.put("money", mememberIntegralBase.getRmb().multiply(whenTimeToDeductPoints));
        return RestResult.success(map);
    }

    /**
     * 统一组装评价-分享-点赞-晒单-收藏赠送表
     *
     * @param memberIntegralRestRequest
     * @param giveIntegralMaxNum
     * @param giveIntegralNum
     */
    @Transactional(rollbackFor = Exception.class)
    public RestResult integralActionType(MemberIntegralRestRequest memberIntegralRestRequest, MememberUser
            mMemberVo, BigDecimal giveIntegralMaxNum, BigDecimal giveIntegralNum, String actionTypeDes,
                                         MememberIntegralBase mememberIntegralBase,
                                         BigDecimal totalNumberOfPoints, BigDecimal totalNumberOfPointsChild) {
        //今日所有积分
        totalNumberOfPoints = totalNumberOfPoints == null ? BigDecimal.ZERO : totalNumberOfPoints;
        totalNumberOfPointsChild = totalNumberOfPointsChild == null ? BigDecimal.ZERO : totalNumberOfPointsChild;
        giveIntegralMaxNum = giveIntegralNum == null ? BigDecimal.ZERO : giveIntegralNum;

        BigDecimal addIntegral = giveIntegralNum.add(totalNumberOfPoints);
        if (mememberIntegralBase.getDailyCeiling() != null) {
            if (addIntegral.compareTo(mememberIntegralBase.getDailyCeiling()) == 1) {
                //平台每日上限
                giveIntegralNum = mememberIntegralBase.getDailyCeiling().subtract(totalNumberOfPoints);
            } else if (addIntegral.compareTo(mememberIntegralBase.getDailyCeiling()) == 0) {
                return RestResult.error(EXCEPTION.FAIL.getName(), actionTypeDes + "失败,赠送积分超出每日上限");
            }
        }
        //当次类型所有积分加上赚送积分
        BigDecimal addIntegralType = giveIntegralNum.add(totalNumberOfPointsChild);

        if (addIntegralType.compareTo(giveIntegralMaxNum) == 1) {
            giveIntegralNum = giveIntegralMaxNum.subtract(totalNumberOfPointsChild);
        }
        BigDecimal changeIntegral = mMemberVo.getIntegral().add(giveIntegralNum);
        //积分流水保存
        MememberIntegralLog mememberIntegralLog = new MememberIntegralLog();
        mememberIntegralLog.setMemberUserId(mMemberVo.getId());
        mememberIntegralLog.setIntegral(giveIntegralNum);
        mememberIntegralLog.setCreateTime(new Date());
        mememberIntegralLog.setOrderId(memberIntegralRestRequest.getOrderId());
        mememberIntegralLog.setChangeMode(Integer.valueOf(memberIntegralRestRequest.getObtainMode()));
        mememberIntegralLog.setActionTypeDes(actionTypeDes);
        mememberIntegralLog.setActionType(2);
        mememberIntegralLog.setChangeTypes(memberIntegralRestRequest.getChangeTypes());
        mememberIntegralLog.setIntegralType(3);
        mememberIntegralLog.setChangeTime(new Date());
        mememberIntegralLog.setFreezeIntegral(changeIntegral);
        mememberIntegralLogMapper.insert(mememberIntegralLog);
        //更新主表总积分
        mMemberVo.setIntegral(changeIntegral);
        mMemberVo.setUpdateTime(new Date());
        mememberUserMapper.update(mMemberVo);
        return RestResult.success("成功");
    }


    /**
     * 会员积分规则下单-充值赠送表
     *
     * @param memberIntegralRestRequest
     * @param mememberIntegralOrderRecharge
     */
    @Transactional(rollbackFor = Exception.class)
    public RestResult orderOrRecharge(MemberIntegralRestRequest memberIntegralRestRequest, MememberUser
            mMemberVo, MememberIntegralOrderRecharge mememberIntegralOrderRecharge, String str,
                                      BigDecimal todayIntegral, MememberIntegralBase mememberIntegralBase, BigDecimal totalScore) {
        //消费标示1：单次消费2：阶梯性消费
        if (mememberIntegralOrderRecharge.getConsumeFlag() == 1) {
            if (memberIntegralRestRequest.getPlaceOrderAmount().compareTo(mememberIntegralOrderRecharge.getConsumeNum()) == -1) {
                return RestResult.error(EXCEPTION.USER_CONSUMPTION.getName(), EXCEPTION.USER_CONSUMPTION.getDesc());
            }
            //判断用户今日积分是否为空
            BigDecimal totalNumberOfPoints = totalScore == null ? BigDecimal.ZERO : totalScore;
            //计算当前赚送积分
            BigDecimal divide = memberIntegralRestRequest.getPlaceOrderAmount().divide(mememberIntegralOrderRecharge.getConsumeNum(), ROUND_DOWN);
            BigDecimal bigDecimal = divide.multiply(mememberIntegralOrderRecharge.getGiveIntegralNum());
            //先计算平台每日上限积分
            if (mememberIntegralBase.getDailyCeiling() != null && bigDecimal.add(totalNumberOfPoints).compareTo(mememberIntegralBase.getDailyCeiling()) != -1) {
                bigDecimal = mememberIntegralBase.getDailyCeiling().subtract((totalNumberOfPoints));
            }
            //当前送的积加上每日获取积分 大于当前每日上限积分
            else if (bigDecimal.add(totalNumberOfPoints).compareTo(mememberIntegralOrderRecharge.getGiveIntegralMaxNum()
                    == null ? BigDecimal.ZERO : mememberIntegralOrderRecharge.getGiveIntegralMaxNum()) != -1) {
                bigDecimal = mememberIntegralOrderRecharge.getGiveIntegralMaxNum().subtract(totalNumberOfPoints);
            }
            BigDecimal integral = mMemberVo.getIntegral() == null ? BigDecimal.ZERO : mMemberVo.getIntegral();
            BigDecimal changeIntegral = integral.add(bigDecimal);
            //积分流水保存
            MememberIntegralLog mememberIntegralLog = new MememberIntegralLog();
            mememberIntegralLog.setMemberUserId(mMemberVo.getId());
            mememberIntegralLog.setIntegral(bigDecimal);
            mememberIntegralLog.setCreateTime(new Date());
            mememberIntegralLog.setChangeMode(memberIntegralRestRequest.getChangeTypes());
            mememberIntegralLog.setOrderId(memberIntegralRestRequest.getOrderId());
            mememberIntegralLog.setChangeMode(Integer.valueOf(memberIntegralRestRequest.getObtainMode()));
            mememberIntegralLog.setActionTypeDes(str);
            mememberIntegralLog.setActionType(2);
            mememberIntegralLog.setChangeTypes(memberIntegralRestRequest.getChangeTypes());
            mememberIntegralLog.setIntegralType(3);
            mememberIntegralLog.setChangeTime(new Date());
            mememberIntegralLog.setFreezeIntegral(changeIntegral);
            mememberIntegralLogMapper.insert(mememberIntegralLog);

            //更新主表总积分
            mMemberVo.setIntegral(changeIntegral);
            mMemberVo.setUpdateTime(new Date());
            mememberUserMapper.update(mMemberVo);

        } else if (mememberIntegralOrderRecharge.getConsumeFlag() == 2) {
            //会员积分规则下单-充值赠送递增表
            List<MememberIntegralOrderIncrement> mememberIntegralOrderIncrementList =
                    mememberIntegralOrderIncrementMapper.query(new MememberIntegralOrderIncrement() {{
                        setDeleteFlag(0);
                        setOrderRechargeId(mememberIntegralOrderRecharge.getId());
                    }});
            if (CollectionUtils.isNotEmpty(mememberIntegralOrderIncrementList)) {

                //积分流水保存
                MememberIntegralLog mememberIntegralLog = new MememberIntegralLog();
                mememberIntegralLog.setMemberUserId(mMemberVo.getId());
                mememberIntegralLog.setCreateTime(new Date());
                mememberIntegralLog.setChangeMode(memberIntegralRestRequest.getChangeTypes());
                mememberIntegralLog.setOrderId(memberIntegralRestRequest.getOrderId());
                mememberIntegralLog.setChangeMode(Integer.valueOf(memberIntegralRestRequest.getObtainMode()));
                mememberIntegralLog.setActionType(2);
                mememberIntegralLog.setActionTypeDes(str);
                mememberIntegralLog.setChangeTypes(memberIntegralRestRequest.getChangeTypes());
                mememberIntegralLog.setIntegralType(3);
                mememberIntegralLog.setChangeTime(new Date());
                mememberIntegralOrderIncrementList.forEach(mememberIntegralOrderIncrement -> {
                    if (memberIntegralRestRequest.getPlaceOrderAmount().compareTo(mememberIntegralOrderIncrement.getConsumeNum()) != -1) {
                        BigDecimal divide = memberIntegralRestRequest.getPlaceOrderAmount().divide(mememberIntegralOrderIncrement.getConsumeNum(), ROUND_DOWN);
                        BigDecimal decimal = divide.multiply(mememberIntegralOrderIncrement.getGiveIntegralNum());
                        //先计算平台每日上限积分
                        if (decimal.add(totalScore == null ? BigDecimal.ZERO : totalScore).compareTo(mememberIntegralBase.getDailyCeiling()) != -1) {
                            mememberIntegralLog.setIntegral(mememberIntegralBase.getDailyCeiling().subtract(totalScore == null ? BigDecimal.ZERO : totalScore));
                        } else if (decimal.add(totalScore == null ? BigDecimal.ZERO : totalScore).
                                compareTo(mememberIntegralOrderIncrement.getGiveIntegralMaxNum()) != -1) {
                            mememberIntegralLog.setIntegral(mememberIntegralOrderIncrement.getGiveIntegralMaxNum().subtract
                                    (totalScore == null ? BigDecimal.ZERO : totalScore));
                        }
                    }
                });
                if (mememberIntegralLog.getIntegral() == null) {
                    return RestResult.error(EXCEPTION.USER_CONSUMPTION.getName(), EXCEPTION.USER_CONSUMPTION.getDesc());
                }
                BigDecimal changeIntegral = mMemberVo.getIntegral().add(mememberIntegralLog.getIntegral());
                mememberIntegralLog.setFreezeIntegral(changeIntegral);
                mememberIntegralLogMapper.insert(mememberIntegralLog);

                //更新主表总积分
                mMemberVo.setIntegral(changeIntegral);
                mMemberVo.setUpdateTime(new Date());
                mememberUserMapper.update(mMemberVo);
            }
        }
        return RestResult.success("成功");
    }
}
